package ce.com.newbluedemo.my;

import android.bluetooth.BluetoothDevice;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import ce.com.cenewbluesdk.CEBC;
import ce.com.cenewbluesdk.proxy.BleFactory;
import ce.com.cenewbluesdk.proxy.CEBluetoothProxyBase;
import ce.com.cenewbluesdk.proxy.CEDevK2Proxy;
import ce.com.cenewbluesdk.proxy.CEDevK6Proxy;
import ce.com.cenewbluesdk.scan.CEScanDevBase;
import ce.com.cenewbluesdk.scan.CEScanDev_3;
import ce.com.cenewbluesdk.scan.CEScanDev_4;
import ce.com.newbluedemo.R;
import ce.com.newbluedemo.k2.TestK2BlueActivity;

public class MainActivity extends MyActivity implements View.OnClickListener {
    ListView listView;
    MyAdapter myAdapter;
    ArrayList<BluetoothDevice> arrayList = new ArrayList<>();
    CEScanDevBase ceScanDevBase;
    private CEBluetoothProxyBase proxy;


    private int devType = 2;

    private Handler uiHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1000) {
                arrayList.add((BluetoothDevice) msg.obj);
            }
            myAdapter.setList(arrayList);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        devType = getIntent().getIntExtra("devType", 5);

        setContentView(R.layout.activity_main);
        findViewById(R.id.end_scan).setOnClickListener(this);
        findViewById(R.id.start_scan).setOnClickListener(this);
        if (3 == devType) {
            ceScanDevBase = new CEScanDev_3(this, new CEScanDevBase.FindBlueTooth() {
                @Override
                public void findDev(BluetoothDevice device, int rssi, byte[] scanRecord) {
                    Log.e("liu", "address=" + device.getAddress());
                    boolean have = false;
                    for (BluetoothDevice b : arrayList
                            ) {
                        if (device.getAddress().equals(b.getAddress())) {
                            have = true;
                            break;
                        }
                    }
                    if (!have) {
                        Message message = new Message();
                        message.what = 1000;
                        message.obj = device;
                        uiHandler.sendMessage(message);
                    }

                }
            });
            proxy = BleFactory.getInstance().getK3Proxy();
        } else {
            ceScanDevBase = new CEScanDev_4(this, new CEScanDevBase.FindBlueTooth() {
                @Override
                public void findDev(BluetoothDevice device, int rssi, byte[] scanRecord) {
                    Log.e("liu", "address=" + device.getAddress());
                    boolean have = false;
                    for (BluetoothDevice b : arrayList
                            ) {
                        if (device.getAddress().equals(b.getAddress())) {
                            have = true;
                            break;
                        }
                    }
                    if (!have) {
                        Message message = new Message();
                        message.what = 1000;
                        message.obj = device;
                        uiHandler.sendMessage(message);
                    }

                }
            });
            switch (devType) {
                case CEDevK2Proxy.devType:
                    proxy = BleFactory.getInstance().getK2Proxy();
                    break;
                case CEDevK6Proxy.devType:
                    proxy = BleFactory.getInstance().getK6Proxy();
                    break;
            }

        }

        listView = (ListView) findViewById(R.id.list_view);
        myAdapter = new MyAdapter(arrayList);
        listView.setAdapter(myAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ceScanDevBase.stopScan();

                proxy.connectDev(arrayList.get(position).getAddress());

            }
        });
        ceScanDevBase.startScan();

    }


    @Override
    public void receiveData(Message message) {
        if (message.what == CEBC.BLUE_CONNECT_STATE_CHANGE) {
            if (message.arg1 == CEBC.BLUE_CONNECTED) {
                Toast.makeText(this, "蓝牙连接成功", Toast.LENGTH_SHORT).show();
                if (devType == 3)
                    startActivity(new Intent(this, TestBlueActivity.class));

                else if (devType == 5) {//K2的参数是 5
                    startActivity(new Intent(this, TestK2BlueActivity.class));
                } else if (devType == 6) {
                    startActivity(new Intent(this, TestK6BlueActivity.class));
                }
                finish();
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_scan:
                ceScanDevBase.startScan();
                arrayList.clear();
                myAdapter.setList(arrayList);

                break;
            case R.id.end_scan:
                ceScanDevBase.stopScan();
                break;
        }
    }

    class MyAdapter extends BaseAdapter {


        public ArrayList<BluetoothDevice> list;


        public MyAdapter(ArrayList<BluetoothDevice> list) {
            this.list = list;
        }

        public void setList(ArrayList<BluetoothDevice> list) {
            this.list = list;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_bluetooth_address, parent, false);
            BluetoothDevice bluetoothDevice = list.get(position);
            TextView name = (TextView) view.findViewById(R.id.name);
            TextView address = (TextView) view.findViewById(R.id.address);
            address.setText(bluetoothDevice.getAddress());
            name.setText(bluetoothDevice.getName());
            return view;
        }
    }


}
